package com.molyfun.parents.activity.organization.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.organization.adapter.SignUpEvaluateAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.SignUpDetailEvaluateBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/11/10 0010.
 */

public class SignUpDetailEvaluateFragment extends BaseFragment implements View.OnClickListener {

    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.submit_button)
    TextView mSubmitButton;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmpty_layout;
    private SignUpEvaluateAdapter mSignUpEvaluateAdapter;
    private String mDepartid;
    private String mEnrollstudnetid;


    public static SignUpDetailEvaluateFragment newInstance(int category, String departid, String enrollstudnetid) {
        SignUpDetailEvaluateFragment studentInfoFragment = new SignUpDetailEvaluateFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("category", category);
        bundle.putString("departid", departid);
        bundle.putString("enrollstudnetid", enrollstudnetid);
        studentInfoFragment.setArguments(bundle);
        return studentInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sign_up_evaluate, container, false);
        ButterKnife.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        mDepartid = getArguments().getString("departid");
        mEnrollstudnetid = getArguments().getString("enrollstudnetid");
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mSignUpEvaluateAdapter = new SignUpEvaluateAdapter(getActivity());
        mRecyclerView.setAdapter(mSignUpEvaluateAdapter);
        mSubmitButton.setOnClickListener(this);
        if(!TextUtils.isEmpty(mDepartid) && !TextUtils.isEmpty(mEnrollstudnetid)) {
            loadData(mDepartid, mEnrollstudnetid);
        }
    }

    private void loadData(String mDepartid, String mEnrollstudnetid) {
        showProgressDialog();
        Call<SignUpDetailEvaluateBean> call = MFPApplication.getInstance().getRestClient().signUpDetailEvaluate(String.valueOf(RestClient.DEFAULT_PAGE), String.valueOf(RestClient.LARGE_PAGE_SIZE), mDepartid, mEnrollstudnetid);
        call.enqueue(new Callback<SignUpDetailEvaluateBean>() {
            @Override
            public void onResponse(Call<SignUpDetailEvaluateBean> call, Response<SignUpDetailEvaluateBean> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    mEmpty_layout.setVisibility(View.GONE);
                    mRecyclerView.setVisibility(View.VISIBLE);
                    mSubmitButton.setVisibility(View.GONE);
                    List<SignUpDetailEvaluateBean.DataBean.RecordsBean> records = response.body().getData().getRecords();
                    mSignUpEvaluateAdapter.refreshData(response.body().getData().getRecords());
                } else {

                }
            }

            @Override
            public void onFailure(Call<SignUpDetailEvaluateBean> call, Throwable t) {
                dismissProgressDialog();
                mSignUpEvaluateAdapter.clearAll();
                mRecyclerView.setVisibility(View.GONE);
                mSubmitButton.setVisibility(View.GONE);
                mEmpty_layout.setVisibility(View.VISIBLE);

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }
}
